package IBBB.view;

import IBBB.model.UserMessage;
import IBBB.model.WordsMessage;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import IBBB.network.sendmessage.send_update_user_schedule;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

public class CompleteWordPane {
    @FXML
    private Button openNoteButton;
    @FXML
    private WebView presentWord;
    @FXML
    private Button nextWordButton;
    @FXML
    private Button exitButton;
    private WebEngine engine;
    private WordsMessage wordsMessage;
    private StackPane container;
    private UserMessage userMessage;
    private boolean isReview;
    private String wordStockName;
    Stage s = new Stage();

    private void warn(String s, String t) {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("WarningPane.fxml"));
        try {
            Parent parent = loader.load();
            WarningPane wp = (WarningPane)loader.getController();
            wp.setMessage(s);
            Stage w = new Stage(StageStyle.UTILITY);
            w.setTitle(t);
            w.setScene(new Scene(parent, 350,220));
            w.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void initialize() {
        engine = presentWord.getEngine();
    }

    public void init(WordsMessage wordsMessage, UserMessage userMessage, StackPane caller, boolean isReview, String name) {
        wordStockName = name;
        this.isReview = isReview;
        container = caller;
        this.userMessage = userMessage;
        this.wordsMessage = wordsMessage;
        engine.loadContent(wordsMessage.getWordProperty().getPresentHtml());
        wordsMessage.getWordProperty().presentHtmlProperty().addListener((observableValue, s, t1) -> engine.loadContent(t1));
    }

    @FXML
    private void setOpenNoteButtonAction() throws IOException {
        openNoteButton.setDisable(true);
        s.setOnCloseRequest((windowEvent -> openNoteButton.setDisable(false)));
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("NotePane.fxml"));
        Parent t = loader.load();
        NotePane c = loader.getController();
        s.setScene(new Scene(t, 700, 420));
        s.show();
    }

    @FXML
    private void setNextWordButtonAction() {
        if(wordsMessage.isFinished()) {
            engine.loadContent("<html><body><br><br><div style=\"text-align:center\"><h1>本次任务完成啦~</h1></div></body></html>");
            nextWordButton.setDisable(true);
        } else {
            wordsMessage.nextWord();
            container.getChildren().remove(2);
            container.getChildren().get(1).setVisible(true);
        }
    }

    @FXML
    private void setExitButtonAction() {
        nextWordButton.setDisable(true);
        s.close();
        if (!isReview) {
            engine.loadContent("<html><div><br><br><h1 style=\"text-align:center\">更新信息中......</h1></div></html>");
            send_update_user_schedule update = new send_update_user_schedule("localhost", wordStockName, userMessage.getUserName().get(), userMessage.getCurrentSchedule(wordStockName) + wordsMessage.getCount());
            Thread t = new Thread(update);
            update.messageProperty().addListener(((observableValue, s, t1) -> {
                switch (t1) {
                    case "timeout": {
                        warn("网络异常更新失败", "警告");
                        container.getChildren().remove(2);
                        container.getChildren().remove(1);
                        container.getChildren().get(0).setVisible(true);
                        break;
                    }
                    case "wrong": {
                        warn("出现异常，更新失败", "警告");
                        container.getChildren().remove(2);
                        container.getChildren().remove(1);
                        container.getChildren().get(0).setVisible(true);
                        break;
                    }
                    case "success": {
                        userMessage.updateSchedule(wordStockName, wordsMessage.getCount());
                        container.getChildren().remove(2);
                        container.getChildren().remove(1);
                        container.getChildren().get(0).setVisible(true);
                        break;
                    }
                }
            }));
            t.start();
        } else {
            container.getChildren().remove(2);
            container.getChildren().remove(1);
            container.getChildren().get(0).setVisible(true);
        }
    }
}
